package ccy.ccysealegg;

import ccy.ccylibrary.item.ItemModifyType;
import ccy.ccylibrary.item.ItemNms;
import ccy.ccylibrary.item.ItemRelevant;
import ccy.ccylibrary.item.nbtTagCompound.NBTTagCompound;
import ccy.ccylibrary.plugin.annotation.Event;
import ccy.ccylibrary.utils.Method;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

import java.util.stream.Collectors;

@Event
public class listener implements Listener {
    config con;

    public listener() {
        this.con = config.con;
    }

    @EventHandler
    public void onUse(PlayerInteractEvent e) {
        ItemStack item = e.getItem();
        if (item != null && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Player player = e.getPlayer();
            World world = player.getWorld();
            if (item.getTypeId() == this.con.ID) {
                if (!world.getName().equals(this.con.useWorld)) {
                    Method.sendmessage(player, this.con.Message);
                    e.setCancelled(true);
                    return;
                }

                player.getWorld().spawnEntity(this.getCorrectLocation(e.getClickedBlock().getLocation(), e.getBlockFace()), EntityType.WITHER_SKELETON);
                item.setAmount(item.getAmount() - 1);
            } else if (item.getTypeId() == this.con.villager_ID) {
                if (!world.getName().equals(this.con.villager_useWorld)) {
                    Method.sendmessage(player, this.con.villager_Message);
                    e.setCancelled(true);
                    return;
                }

                player.getWorld().spawnEntity(this.getCorrectLocation(e.getClickedBlock().getLocation(), e.getBlockFace()), EntityType.VILLAGER);
                item.setAmount(item.getAmount() - 1);
            } else {
                NBTTagCompound tagCompound = ItemNms.getTagCompound(item);
                if (tagCompound != null) {
                    String entityData = ItemNms.getTagCompound(item).getString("EntityData");
                    if (!entityData.equals("")) {
                        if (!Method.isAllowUse(world.getName(), this.con.World)) {
                            player.getWorld().spawnEntity(this.getCorrectLocation(e.getClickedBlock().getLocation(), e.getBlockFace()), EntityType.valueOf(entityData));
                            item.setAmount(item.getAmount() - 1);
                            Method.sendmessage(player, this.con.MessageSpawn.replace("<entity>", this.con.EntityNameMap.get(entityData)));
                        } else {
                            Method.sendmessage(player, this.con.MessageWorld);
                        }
                    }
                }
            }
        }

    }

    private Location getCorrectLocation(Location location, BlockFace blockFace) {
        location.setX(location.getX() + (double)0.5F);
        location.setZ(location.getZ() + (double)0.5F);
        switch (blockFace) {
            case UP:
                location.setY(location.getY() + (double)1.0F);
                break;
            case DOWN:
                location.setY(location.getY() - (double)1.0F);
                break;
            case SOUTH:
                location.setZ(location.getZ() + (double)1.0F);
                break;
            case NORTH:
                location.setZ(location.getZ() - (double)1.0F);
                break;
            case WEST:
                location.setX(location.getX() - (double)1.0F);
                break;
            case EAST:
                location.setX(location.getX() + (double)1.0F);
        }

        return location;
    }

    @EventHandler
    public void onInteractEntity(PlayerInteractEntityEvent e) {
        Entity entity = e.getRightClicked();
        ItemStack item = e.getPlayer().getItemInHand();
        if (entity != null && item != null && e.getHand() == EquipmentSlot.HAND) {
            Player player = e.getPlayer();
            if (ItemRelevant.isCorrectItem(this.con.SealItem, item)) {
                if (!Method.isAllowUse(player.getWorld().getName(), this.con.World)) {
                    if (!Method.isAllowUse(entity.getType().name(), this.con.EntityList)) {
                        NBTTagCompound tagCompound = ItemNms.getTagCompound(item);
                        if (tagCompound.getString("EntityData").isEmpty()) {
                            entity.remove();
                            if (entity.isDead()) {
                                item.setAmount(item.getAmount() - 1);
                                tagCompound.setString("EntityData", entity.getType().name());
                                ItemStack itemStack = tagCompound.setTagCompound();
                                itemStack.setAmount(1);
                                String name = this.con.EntityNameMap.get(entity.getType().name());
                                player.getInventory().addItem(ItemRelevant.ItemInfoModify(ItemModifyType.addLore, itemStack, this.con.SealLore.stream().map((s) -> Method.ColorReplace(s.replace("<entity>", name))).collect(Collectors.toList())));
                                Method.sendmessage(player, this.con.MessageSeal.replace("<entity>", name));
                            }
                        }
                    } else {
                        Method.sendmessage(player, this.con.MessageNotSeal);
                    }
                } else {
                    Method.sendmessage(player, this.con.MessageWorld);
                }
            }
        }

    }
}
